package com.fzu.orderservice.config;

import com.fzu.commonutil.entity.Company;
import com.fzu.commonutil.params.DataResult;
import com.fzu.commonutil.util.CodeEnum;
import com.fzu.commonutil.util.RedisKey;
import com.fzu.commonutil.util.RedisUtil;
import com.fzu.orderservice.client.NormalClient;
import com.fzu.orderservice.service.OrderFormService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Configuration
@EnableScheduling
@Slf4j
@RequiredArgsConstructor
public class TimeTask {

    private final OrderFormService orderFormService;
    private final RedisUtil redisUtil;
    private final NormalClient normalClient;

    /**
     * 更新数据大屏数据，每30分钟执行一次
     */
    @Scheduled(fixedRate = 30 * 60 * 1000)
    public void timeout() throws ExecutionException, InterruptedException {
        log.info("【数据大屏订单数据开始生成...】");
        CompletableFuture<DataResult> future1 = orderFormService.asyncTask2(null, CodeEnum.MANAGER.getCode());
        CompletableFuture<DataResult> future2 = orderFormService.asyncTask1(null);
        List<CompletableFuture<DataResult>> future1List = new ArrayList<>();
        List<CompletableFuture<DataResult>> future2List = new ArrayList<>();
        List<Company> companies = normalClient.allCompany();
        for (Company company : companies) {
            CompletableFuture<DataResult> f1 = orderFormService.asyncTask2(company.getId(), CodeEnum.COMPANY.getCode());
            CompletableFuture<DataResult> f2 = orderFormService.asyncTask1(company.getId());
            future1List.add(f1);
            future2List.add(f2);
        }

        for (int i = 0; i < future1List.size(); i++) {
            CompletableFuture<DataResult> f1 = future1List.get(i);
            CompletableFuture<DataResult> f2 = future2List.get(i);
            DataResult data = new DataResult();
            DataResult r1 = f1.get();
            DataResult r2 = f2.get();
            data.setTypeData(r1.getTypeData());
            data.setTypeLabel(r1.getTypeLabel());
            data.setOrderCount(r1.getOrderCount());
            data.setOrderData(r2.getOrderData());
            data.setOrderLabel(r2.getOrderLabel());
            redisUtil.set(RedisKey.ORDER_DATA + "_" + r1.getCompanyId(), data);
        }

        DataResult result1 = future1.get();
        DataResult result2 = future2.get();
        DataResult dataResult = new DataResult();
        dataResult.setTypeData(result1.getTypeData());
        dataResult.setTypeLabel(result1.getTypeLabel());
        dataResult.setOrderCount(result1.getOrderCount());
        dataResult.setOrderData(result2.getOrderData());
        dataResult.setOrderLabel(result2.getOrderLabel());
        redisUtil.set(RedisKey.ORDER_DATA, dataResult);
        log.info("【数据大屏订单数据生成完成！】");
    }
}
